<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//用户社区私信表
class Bbs_dialog_model extends CI_Model{
    function __construct() {
        parent::__construct();
        $this->users_info = "users_info";
        $this->bbs_user_dialog_message = 'bbs_user_dialog_message';
        $this->bbs_user_dialog_room = 'bbs_user_dialog_room';
    }
    
    //获取用户聊天室总数
    function get_dialog_room_count($where){
        $this->db->select('*');
        $this->db->from($this->bbs_user_dialog_room);
        $this->db->where($where);
        return $this->db->get()->num_rows();
    }
    
    //获取聊天室最新消息列表
    function get_dialog_room_message($user_id,$page,$limit){
        $sql = "SELECT * FROM (SELECT room_name,content,create_time FROM tan_bbs_user_dialog_message WHERE (sender_from_id=$user_id) OR (sender_to_id=$user_id AND is_show=0) ORDER BY create_time DESC) a GROUP BY room_name ORDER BY `a`.`create_time` DESC limit $page,$limit";
        $list = $this->db->query($sql)->result_array();
        return $list;
    }
    
    //获取聊天室信息
    function get_dialog_room($where){
        return $this->db->select($this->bbs_user_dialog_room.".*,".$this->users_info.".nickname")->from($this->bbs_user_dialog_room)->join($this->users_info,$this->bbs_user_dialog_room.".dialog_to = ".$this->users_info.".user_id","left")->where($where)->get()->row_array();
    }
    //插入聊天室信息
    function insert_dialog_room($data){
        $this->db->insert_batch($this->bbs_user_dialog_room, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    function update_dialog_room($where,$data){
        return $this->db->where($where)->update($this->bbs_user_dialog_room, $data);
    }
    
    function get_message_count($where){
        $this->db->select('*');
        $this->db->from($this->bbs_user_dialog_message);
        $this->db->where($where);
        return $this->db->get()->num_rows();
    }
    
    function get_message($where){
        
        $this->db->select($this->bbs_user_dialog_message.'.*');
        $this->db->from($this->bbs_user_dialog_message);
        $this->db->where($where);
        $this->db->order_by($this->bbs_user_dialog_message.".create_time DESC");
        return $this->db->get()->row_array();
    }
    
    function get_message_list($where,$page,$limit){
        
        $this->db->select($this->bbs_user_dialog_message.'.*');
        $this->db->from($this->bbs_user_dialog_message);
        $this->db->where($where);
        $this->db->order_by($this->bbs_user_dialog_message.".create_time DESC");
        $this->db->limit($limit,$page);
        return $this->db->get()->result_array();
    }
    
    //添加聊天信息记录
    function insert_message($data){
        $this->db->insert($this->bbs_user_dialog_message, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    
    //批量添加聊天信息记录
    function insert_message_batch($data){
        $this->db->insert_batch($this->bbs_user_dialog_message, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    
    //更新聊天记录信息
    function update_message($where,$data){
        return $this->db->where($where)->update($this->bbs_user_dialog_message, $data);
    }
}
